package com.bootdo.ocr.baidu.dto;

import com.bootdo.ocr.OcrBaseResult;
import com.google.common.collect.Sets;
import com.google.gson.annotations.SerializedName;
import org.apache.commons.lang3.StringUtils;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Pattern;

public class BaiduOcrResult extends OcrBaseResult<BaiduOcrResult> {

    @SerializedName("log_id")
    private String logId;

    @SerializedName("words_result")
    private List<WordInfo> wordsResult;

    @SerializedName("words_result_num")
    private int wordsResultNum;

    public String getLogId() {
        return logId;
    }

    public void setLogId(String logId) {
        this.logId = logId;
    }

    public List<WordInfo> getWordsResult() {
        return wordsResult;
    }

    public void setWordsResult(List<WordInfo> wordsResult) {
        this.wordsResult = wordsResult;
    }

    public int getWordsResultNum() {
        return wordsResultNum;
    }

    public void setWordsResultNum(int wordsResultNum) {
        this.wordsResultNum = wordsResultNum;
    }
    /**
     *  将文件名称 中的特殊字符转成 编码统一字符
     * @return
     */
//    public static String replaceChar2CodeSymbol() {
//
//    }

    @Override
    public String fetchGbCode(String pattern) {
        Set<String> gbCodeList = Sets.newHashSet();
        String patternVal = replacePatternBySpecialMap(pattern);
        if (wordsResultNum == 0) {
            return null;
        }
        for (WordInfo wordInfo : wordsResult) {
            String code = matchWord(wordInfo.words, Pattern.compile(patternVal));
            code = filterUpdateCode(code);
            if (StringUtils.isNotEmpty(code)) {
                gbCodeList.add(code);
            }
         }
       Optional<String>  optional = gbCodeList
            .stream()
                .max((o1, o2) ->
                    {
                        if (o1.length() > o2.length()) {
                            return  1;
                        } else if(o1.length() < o2.length()) {
                            return -1;
                        }
                        return o1.compareTo(o2);
                     });
        return optional.isPresent() ? optional.get() : null;
    }


    static class WordInfo {
        public WordInfo() {
        }
        private String words;
        public String getWords() {
            return words;
        }
        public void setWords(String words) {
            this.words = words;
        }
    }
}
